﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FPackage : FormBase
    {
        Package aPackage = null;
        Validations valid = new Validations();
        public enum ACTION { VIEW, SEARCH, MODIFY, CREATE, ADDWORK, DELETE, ADDTOAUCTION, ADDFROMAUCTION };
        ACTION actionActual = ACTION.VIEW;
        private EDM_AuctionContainer context;
        private Auction auction;
        
        

        public FPackage(ACTION anAction, Package aPackage)
        {
            InitializeComponent();
            actionActual = anAction;
            this.aPackage = aPackage;
        
        }

        public FPackage(ACTION aCTION, EDM_AuctionContainer context, Auction auction)
        {
            // TODO: Complete member initialization
            actionActual = aCTION;
            this.context = context;
            this.auction = auction;
            InitializeComponent();
        }

        public FPackage(ACTION aCTION, EDM_AuctionContainer context)
        {
            InitializeComponent();
            actionActual = aCTION;
            this.context = context;
        }

        private void FPackage_Load(object sender, EventArgs e)
        {
            preparePanel();
            loadDataGrid();
        }

        private void preparePanel()
        {
            if (actionActual == ACTION.VIEW)
            {
                //this.BackColor = Color.LightGreen;
                b_save.Visible = false;
                b_addMore.Visible = false;
                pnlSearchPackage.Visible = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            if (actionActual == ACTION.SEARCH)
            {
                //this.BackColor = Color.LightGreen;
                b_save.Visible = false;
                b_addMore.Visible = false;

                pnlSearchPackage.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }

            if (actionActual == ACTION.DELETE)
            {
                b_save.Visible = false;
                b_addMore.Visible = false;

                pnlSearchPackage.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            else if (actionActual == ACTION.MODIFY)
            {
                b_save.Visible = true;
                b_addMore.Visible = true;
                this.ControlesHabilitados(false);
            }

            else if (actionActual == ACTION.CREATE)
            {
                b_save.Visible = true;
                b_addMore.Visible = true;
                this.ControlesHabilitados(true);
            }

            else if (actionActual == ACTION.ADDWORK)
            {
                b_save.Visible = true;
                b_addMore.Visible = false;
                this.ControlesHabilitados(true);
            }

            else if (actionActual == ACTION.ADDTOAUCTION)
            {
                b_save.Text = "Add to Auction";
                b_save.Visible = true;
                b_addMore.Visible = false;
                this.ControlesHabilitados(true);
            }

            if (actionActual == ACTION.SEARCH || actionActual == ACTION.MODIFY || actionActual == ACTION.ADDTOAUCTION || actionActual == ACTION.DELETE)
            {
                FSearchPackage f_SearchPackage = new FSearchPackage(this, context);
                f_SearchPackage.TopLevel = false;
                pnlSearchPackage.Controls.Add(f_SearchPackage);
                f_SearchPackage.Show();
            }
            else if (actionActual == ACTION.ADDFROMAUCTION)
            {
                pnlSearchPackage.Visible = true;
                FSearchPackage f_SearchPackage = new FSearchPackage(this, context);
                f_SearchPackage.TopLevel = false;
                pnlSearchPackage.Controls.Add(f_SearchPackage);
                f_SearchPackage.Show();
            }
          
        }

        public override void TransferPackageFound(Package aPackage)
        {
            this.aPackage = aPackage;
            b_save.Visible = true;
            this.ControlesHabilitados(true);
            pnlSearchPackage.Visible = false;
            LoadDataOfPackage();
            loadDataGrid();
            if (actionActual == ACTION.DELETE)
            {
                b_save.Visible = false;
                this.ControlesHabilitados(false);
                b_deletePackage.Visible = true;
                b_addMore.Visible = false;
                dG_Packages.Enabled = false;
            }
        }

        private void LoadDataOfPackage()
        {
            if (this.aPackage != null)
            {
                t_minimun.Text = aPackage.minimunSellingPrice.ToString();
                t_name.Text = aPackage.nameOfPackage;
                t_Sold.Text = aPackage.sold.ToString();
                t_outputPrice.Text = aPackage.outputPrice.ToString();
                t_description.Text = aPackage.description;
                b_addMore.Visible = true;
                //QUEDA RELLENAR EL DATAGRID
            }
        }

        private void loadDataGrid()
        {
            int x = 0;
            if (aPackage != null)
            {
                if (aPackage.WorkOfArt != null)
                {
                    //hacer procedimiento

                    foreach (WorkOfArt aWork in aPackage.WorkOfArt)
                    {
                        dG_Packages.Rows.Add();
                        dG_Packages.Rows[x].Cells[Author.Index].Value = aWork.AuthorName;
                        dG_Packages.Rows[x].Cells[id_Package.Index].Value = aWork.Package.idPackage;
                        dG_Packages.Rows[x].Cells[id.Index].Value = aWork.idWorkOfArt;
                        dG_Packages.Rows[x].Cells[nameOfWork.Index].Value = aWork.nameOfWork;
                        dG_Packages.Rows[x].Cells[author_surname.Index].Value = aWork.AuthorSurname;
                        dG_Packages.Rows[x].Cells[year.Index].Value = aWork.year;
                        x++;
                        b_deleteWork.Visible = true;
                    }
                }
            }
            if (x == 0)
            {
                l_NoWorks.Visible = true;
                l_list.Visible = false;
               // b_save.Visible = false;
            }
            else 
            {
                l_NoWorks.Visible = false;
                l_list.Visible = true;
                //b_save.Visible = true;
            }
        }

        private void b_save_Click(object sender, EventArgs e)
        {
            Output(DialogResult.OK);
            Close();
        }

        private void Output(DialogResult Salida)
        {
            switch (Salida)
            {
                case DialogResult.OK:
                    if (CheckData())
                    {
                        aPackage.description = t_description.Text;
                        aPackage.minimunSellingPrice = valid.GetDecimal(t_minimun);
                        aPackage.nameOfPackage = t_name.Text;
                        aPackage.outputPrice = valid.GetDecimal(t_outputPrice);
                        if (t_Sold.Text == "Yes" || t_Sold.Text == "YES" || t_Sold.Text == "yes")
                        {
                            aPackage.sold = true;
                        }
                        else { aPackage.sold = false; }

                        if (actionActual == ACTION.CREATE)
                        {
                            context.PackageSet.AddObject(aPackage);
                            context.SaveChanges();
                        }
                        else
                        {
                            context.SaveChanges();
                        }
                        MessageBox.Show("The package has been saved", "New Package", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else 
                    {
                        MessageBox.Show("You must fill all data", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
               
                case DialogResult.Retry:
                    {
                        context.PackageSet.DeleteObject(aPackage);
                        context.SaveChanges();
                        MessageBox.Show("The package has been deleted", "Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    }

                case DialogResult.Cancel:
                    {
                        MessageBox.Show("The package has not been saved", "Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    }
            }
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Cancel);
            Close();
        }
        private void b_addMore_Click(object sender, EventArgs e)
        {
            DialogResult Salida;
            if (CheckData())
            {
                if (actionActual == ACTION.CREATE)
                {
                    aPackage = new Package();
                    actionActual = ACTION.ADDWORK;
                    aPackage.description = t_description.Text;
                    aPackage.minimunSellingPrice = valid.GetDecimal(t_minimun);
                    aPackage.nameOfPackage = t_name.Text;
                    aPackage.outputPrice = valid.GetDecimal(t_outputPrice);
                    if (t_Sold.Text == "Yes" || t_Sold.Text == "YES" || t_Sold.Text == "yes")
                    {
                        aPackage.sold = true;
                    }
                    else { aPackage.sold = false; }
                    context.PackageSet.AddObject(aPackage);
                    context.SaveChanges();
                }
              
                FAddWorkToPackage faddwork = new FAddWorkToPackage(context, aPackage);
                Salida = faddwork.ShowDialog();
                switch (Salida)
                {
                    case DialogResult.OK:
                        TransferPackageFound(aPackage);
                        break;

                    case DialogResult.Cancel:
                        break;
                }
            }
            
            else { MessageBox.Show("You must fill in all data", "Operation cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information); }
        }
        private bool CheckData()
        {
            bool correct = true;
            if (t_description.Text == "")
            {
                return false;
            }
            else if (t_minimun.Text == "")
            {
                return false;
            }
            else if (t_name.Text == "")
            {
                return false;
            }
            else if (t_outputPrice.Text == "")
            {
                return false;
            }
            else if (t_Sold.Text == "")
            {
                return false;
            }
            return correct;
        }

        private void b_deleteWork_Click(object sender, EventArgs e)
        {
            int row = dG_Packages.CurrentRow.Index;
            int idSelected=valid.toInt(dG_Packages.Rows[row].Cells[0].Value.ToString());
            try
            {
                WorkOfArt selected = context.WorkOfArtSet.FirstOrDefault(w => w.idWorkOfArt == idSelected);
                aPackage.WorkOfArt.Remove(selected);//delete of entity package
                context.SaveChanges();
                dG_Packages.Rows.RemoveAt(row); //delete of list
            }
            catch (System.Exception)
            {
                MessageBox.Show("You must select a row with works of art", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }  
        }

        private void dG_Packages_Click(object sender, EventArgs e)
        {
            if (dG_Packages.CurrentRow.Index > -1)
            {
                b_deleteWork.Enabled = true;
            }
            else 
            {
                b_deleteWork.Enabled = false;
            }
        }

        private void b_deletePackage_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Retry);
            Close();
        }
    }
}
